package com.atzongmaotan.reggie.controller;

import com.atzongmaotan.reggie.common.R;
import com.atzongmaotan.reggie.dto.DishDto;
import com.atzongmaotan.reggie.entity.Category;
import com.atzongmaotan.reggie.entity.Dish;
import com.atzongmaotan.reggie.entity.DishFlavor;
import com.atzongmaotan.reggie.service.CategoryService;
import com.atzongmaotan.reggie.service.DishFlavorService;
import com.atzongmaotan.reggie.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:    DishController
 * Package:  com.atzongmaotan.controller
 * Description:
 *
 * @Author: interface
 * @Create: 2024/6/14 - 上午7:15
 * @Version: v1.0
 */
@RestController
@Slf4j
@RequestMapping("dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto) {
        log.info("addDish{}",dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }
    @GetMapping("page")
    public R<Page<DishDto>> getDishPage(@RequestParam(defaultValue = "1") Integer page,Integer pageSize,String name){
        log.info("getDishPage{}",page);
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name),Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageInfo, queryWrapper);


        Page<DishDto> dishDtoPage = new Page<>();
        //copy pageInfo对象属性,省略 records
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");

        List<Dish> records = pageInfo.getRecords();

        List<DishDto> dishDtoList = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(dishDtoList);

        return R.success(dishDtoPage);
    }
    @GetMapping("{id}")
    public R<DishDto> getDishById(@PathVariable("id") Long id) {
        log.info("getDishById{}",id);
        DishDto dishDto = dishService.getDishDtoById(id);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String> updateDish(@RequestBody DishDto dishDto) {
        log.info("updateDish{}",dishDto.toString());
        dishService.updateWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }
    /*
        @GetMapping("list")
        public R<List<Dish>> getDishList(Dish dish){
            log.info("getDishList{}",dish.toString());
            LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
            //查询状态为 1 (起售状态) 的菜品
            queryWrapper.eq(Dish::getStatus,1);
            //排序条件
            queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

            List<Dish> dishList = dishService.list(queryWrapper);
            return R.success(dishList);
        }
    */

    @GetMapping("list")
    public R<List<DishDto>> getDishList(Dish dish){
        log.info("getDishList{}",dish.toString());
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        //查询状态为 1 (起售状态) 的菜品
        queryWrapper.eq(Dish::getStatus,1);
        //排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> dishList = dishService.list(queryWrapper);
        List<DishDto> dishDtoList = dishList.stream().map((item)->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category!=null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            //当前菜品的ID
            Long dishId = item.getId();
            //获取口味列表
            LambdaQueryWrapper<DishFlavor> dishFlavorQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorQueryWrapper.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorQueryWrapper);
            dishDto.setFlavors(dishFlavorList);

            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtoList);
    }


}
